<?php
/**
 * @author wyl
 * @date 2015-8-4 16:29:04
 */
Yii::import('system.web.widgets.CWidget');
Yii::import('application.components.widgets.CButtons');
class CDialogBAK extends CWidget{
	
	public $title;
	public $buttons=array();
	public $closebuttonCssClass='btn btn-default';
	public $closebuttonTitle='close';
	public $headerCssClass='modal-header modal-header-mine';
	public $isfade='fade';
	public $footerCssClass='modal-footer';
	
	public function init(){
		echo $this->headerHtml();
		echo $this->renderHeader();
		echo $this->renderBody();
	}
	
	protected function renderHeader(){
		$html=<<<header
		<div class="{$this->headerCssClass}">
		        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
		        <span aria-hidden="true">&times;</span></button>
		        <h4 class="modal-title">{$this->title}</h4>
       </div>
header;
		return $html;
	}
	protected function renderBody(){
		$html=<<<body
		<div class="modal-body">
body;
		return $html;
	}
	protected function renderFooter(){
		$html=<<<footer
		</div>
		<div class="{$this->footerCssClass}">
	       
footer;
		if(!empty($this->buttons)){
			foreach ($this->buttons as $buttonId => $buttonConfig){
				$button=new CButtons();
			
				if (!array_key_exists('type',$buttonConfig)) {
					$buttonConfig['type'] = 'button';
				}
				
				$button->id = /* $this->getId().'-'. */$buttonId;
				$button->title=!empty($buttonConfig['title'])?$buttonConfig['title']:$buttonId;
				
				if(array_key_exists('click',$buttonConfig)){
					$button->click=$buttonConfig['click'];
				}
				
				if(array_key_exists('class',$buttonConfig)){
					$button->class=$buttonConfig['class'];
				}
		
				switch ($buttonConfig['type']){
					case 'close':
						$button->htmlOptions['data-dismiss'] = 'modal';
						break;
					case 'button':
						
						
						break;
				}
				
				$html.=$button->renderButton();
			}
		}
		return $html;
	}
	
	protected function headerHtml(){
		$html = <<<headerHtml
		<div class="modal {$this->isfade} component component-dialog" role='dialog' id='{$this->getId()}' aria-labelledby="myModalLabel">
		  <div class="modal-dialog">
		    <div class="modal-content">
headerHtml;
		return $html;
	}
	
	protected function endHtml(){
		$html = "</div></div></div></div>";

		return $html;
	}
	
	
	public function run(){
		echo $this->renderFooter();
		echo $this->endHtml();	
		Yii::app()->clientScript->registerCss('css1',"
			.btn.btn-choose{
				background-color:#1E2225;
				color:#fff;
				border-color:#1E2225;
				padding:6px 25px 6px 25px;
				margin-top:15px;
			}
			.form-horizontal{
				margin-top:30px;
			}
			.modal-header.modal-header-mine{
				background-color:#1E2225;
				color:#fff;
			}
			.modal-header.modal-header-mine span{
				color:#fff;
			}		
		");
		Yii::app()->clientScript->registerScript($this->getId(),"
			var dialog = function(modalBody,top){
				modalBody.find('.modal-dialog').css({ 
	  	  			'-webkit-transition':'-webkit-transform .4s ease-out',
	  			    '-o-transition'		:'-o-transform .4s ease-out',
	  		    	'transition'		:'transform .4s ease-out',
					'-webkit-transform'	:'translate(0,'+top+')',
					'-ms-transform'		:'translate(0,'+top+')',
					'-o-transform'		:'translate(0,'+top+')',
					'transform'			:'translate(0,'+top+')'
	  	  		});
			}  
	      	$('#".$this->getId()."').on('shown.bs.modal',function(e){
				var wHeight	= $(window).height();
	  			var dHeight	= $(this).find('.modal-dialog').height();
	  			var dTop 	= (wHeight/2 - dHeight/2)+'px';
	  			dialog($(this),dTop);
			});
	      	$('#".$this->getId()."').on('hidden.bs.modal',function(e){
	      		$(this).find('.modal-dialog').removeAttr('style');
			});
		");
	}
}